package cn.iocoder.yudao.module.peizhen.controller.admin.buiessorder;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.peizhen.controller.admin.buiessorder.vo.*;
import cn.iocoder.yudao.module.peizhen.dal.dataobject.buiessorder.BuiessOrderDO;
import cn.iocoder.yudao.module.peizhen.service.buiessorder.BuiessOrderService;

@Tag(name = "管理后台 - 订单")
@RestController
@RequestMapping("/peizhen/buiess-order")
@Validated
public class BuiessOrderController {

    @Resource
    private BuiessOrderService buiessOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建订单")
    @PreAuthorize("@ss.hasPermission('peizhen:buiess-order:create')")
    public CommonResult<Long> createBuiessOrder(@Valid @RequestBody BuiessOrderSaveReqVO createReqVO) {
        return success(buiessOrderService.createBuiessOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订单")
    @PreAuthorize("@ss.hasPermission('peizhen:buiess-order:update')")
    public CommonResult<Boolean> updateBuiessOrder(@Valid @RequestBody BuiessOrderSaveReqVO updateReqVO) {
        buiessOrderService.updateBuiessOrder(updateReqVO);
        return success(true);
    }

    @PutMapping("/updateStaff")
    @Operation(summary = "指派服务人员")
    @PreAuthorize("@ss.hasPermission('peizhen:buiess-order:update')")
    public CommonResult<Boolean> updateBuiessOrderStaff(@Valid @RequestBody BuiessOrderSaveReqVO updateReqVO) {
        buiessOrderService.updateBuiessOrderStaff(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('peizhen:buiess-order:delete')")
    public CommonResult<Boolean> deleteBuiessOrder(@RequestParam("id") Long id) {
        buiessOrderService.deleteBuiessOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('peizhen:buiess-order:query')")
    public CommonResult<BuiessOrderRespVO> getBuiessOrder(@RequestParam("id") Long id) {
        BuiessOrderDO buiessOrder = buiessOrderService.getBuiessOrder(id);
        return success(BeanUtils.toBean(buiessOrder, BuiessOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得订单分页")
    @PreAuthorize("@ss.hasPermission('peizhen:buiess-order:query')")
    public CommonResult<PageResult<BuiessOrderRespVO>> getBuiessOrderPage(@Valid BuiessOrderPageReqVO pageReqVO) {
        PageResult<BuiessOrderDO> pageResult = buiessOrderService.getBuiessOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BuiessOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订单 Excel")
    @PreAuthorize("@ss.hasPermission('peizhen:buiess-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBuiessOrderExcel(@Valid BuiessOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BuiessOrderDO> list = buiessOrderService.getBuiessOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订单.xls", "数据", BuiessOrderRespVO.class,
                        BeanUtils.toBean(list, BuiessOrderRespVO.class));
    }

}